﻿using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UsedBook.Models;
using UsedBook.Utils;

namespace UsedBook.DAL
{
    public class MajorDAL
    {
        // 获取某个学院的所有专业
        public List<Major> GetMajorsByCollegeId(int collegeId)
        {
            using(SqlConnection conn = DBHelper.GetSqlConnection())
            {
                conn.Open();
                string sql = "select * from Major where CollegeId=@id";
                SqlParameter sqlParameter = new SqlParameter("id",collegeId);
                List<Major> majors = new List<Major>(); 
                using (SqlCommand cmd = new(sql,conn))
                {
                    cmd.Parameters.Add(sqlParameter);
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        Major major = new Major();
                        major.MajorName = reader.GetString(reader.GetOrdinal("MajorName"));
                        major.MajorId = reader.GetInt32(reader.GetOrdinal("MajorId"));
                        major.CollegeId = reader.GetInt32(reader.GetOrdinal("CollegeId"));
                        majors.Add(major);
                    }
                    return majors;
                }
            }
        }

        // 添加专业
        public bool AddMajor(Major major)
        {


        }

    }
}
